package com.youu.youu.manager.shiro;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.config.Ini;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.config.IniFilterChainResolverFactory;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;

import com.youu.youu.manager.dao.TPermissMapper;
import com.youu.youu.manager.service.RolesBackstageService;

public class ShiroPermissionFactory extends ShiroFilterFactoryBean {

	/** 配置中的过滤链 */
	public static String definitions;

	/** 权限service */
	@Autowired
	TPermissMapper tPermissMapper;

	@Autowired
	RolesBackstageService rBackstageService;

	/**
	 * 从数据库动态读取权限
	 */
	@Override
	public void setFilterChainDefinitions(String definitions) {
		ShiroPermissionFactory.definitions = definitions;
		String[] split = definitions.split("=");
		HashMap<String, String> perHashMap = new LinkedHashMap<>();
		perHashMap.put(split[0], split[1]);
		// 数据库动态权限
		// List<TRole> roles = rBackstageService.getAllRoles();
		// for (TRole tRole : roles) {
		// String roleName = tRole.getRoleName();
		// List<TPermiss> tPermisses =
		// tPermissMapper.selectPermissByOneRole(tRole);
		// if (tPermisses != null) {
		// for (TPermiss tPermiss : tPermisses) {
		// String url = tPermiss.getUrl();
		// if (!StringUtils.isEmpty(url)) {
		// definitions = definitions + url + " = "+"roles["+ roleName +"]";
		// }
		// }
		// }
		// }
		// 从配置文件加载权限配置
		// Ini ini = new Ini();
		// ini.load(definitions);
		// Ini.Section section = ini
		// .getSection(IniFilterChainResolverFactory.URLS);
		// if (CollectionUtils.isEmpty(section)) {
		// section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
		// }
		// 加入权限集合
		Map<String, String> map = getFilterChainDefinitionMap();
		perHashMap.putAll(map);
		System.out.println("权限又重新设置了----》" + perHashMap);
		
		setFilterChainDefinitionMap(perHashMap);
	}

}
